package com.himiotech.core.project.web.system.monitor;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.himiotech.core.framework.helper.service.CacheService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 缓存监控
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Controller
@RequestMapping("/monitor/cache")
public class CacheController extends HimioBaseController {

    @Autowired
    private CacheService cacheService;

    @GetMapping
    @RequiresPermissions("monitor:cache:view")
    public String cache() {
        return "monitor/cache";
    }

    @PostMapping("/getNames")
    @RequiresPermissions("monitor:cache:listNames")
    @ResponseBody
    public HimioResult getCacheNames(String searchCacheName) {
        String[] cacheNames = cacheService.getCacheNames();
        List<String> cacheNameList = Arrays.stream(cacheNames).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(searchCacheName)) {
            List<Map<String, String>> cacheNameMapList = cacheNameList.stream()
                    .filter(name -> name.contains(searchCacheName))
                    .map(s -> MapUtil.of("cacheName", s)).collect(Collectors.toList());
            return writeJsonLayuiTable(cacheNameMapList);
        } else {
            List<Map<String, String>> cacheNameMapList = cacheNameList.stream()
                    .map(s -> MapUtil.of("cacheName", s)).collect(Collectors.toList());
            return writeJsonLayuiTable(cacheNameMapList);
        }
    }

    @PostMapping("/getKeys")
    @RequiresPermissions("monitor:cache:listKeys")
    @ResponseBody
    public HimioResult getCacheKeys(String cacheName, String searchCacheKey) {
        Set<String> cacheKeys = cacheService.getCacheKeys(cacheName);
        List<String> cacheKeyList = new ArrayList<>(cacheKeys);
        if (StringUtils.isNotEmpty(searchCacheKey)) {
            List<Map<String, String>> cacheKeyMapList = cacheKeyList.stream()
                    .filter(name -> name.contains(searchCacheKey))
                    .map(s -> MapUtil.of("cacheKey", s)).collect(Collectors.toList());
            return writeJsonLayuiTable(cacheKeyMapList);
        } else {
            List<Map<String, String>> cacheKeyMapList = cacheKeyList.stream()
                    .map(s -> MapUtil.of("cacheKey", s)).collect(Collectors.toList());
            return writeJsonLayuiTable(cacheKeyMapList);
        }
    }

    @PostMapping("/getValue")
    @ResponseBody
    public HimioResult getCacheValue(String cacheName, String cacheKey) {
        Map<String, Object> result = MapUtil.of("cacheName", cacheName);
        result.put("cacheKey", cacheKey);
        Object cacheValue = cacheService.getCacheValue(cacheName, cacheKey);
        try {
            String res = JSONUtil.toJsonPrettyStr(cacheValue);
            result.put("cacheValue", res);
        } catch (Exception e) {
            result.put("cacheValue", cacheValue);
        }
        return writeJsonOkData(result);
    }

    @PostMapping("/clearCacheName")
    @RequiresPermissions("monitor:cache:clear")
    @ResponseBody
    public HimioResult clearCacheName(String cacheName) {
        cacheService.clearCacheName(cacheName);
        return writeJsonOk();
    }

    @PostMapping("/clearCacheKey")
    @RequiresPermissions("monitor:cache:clear")
    @ResponseBody
    public HimioResult clearCacheKey(String cacheName, String cacheKey) {
        cacheService.clearCacheKey(cacheName, cacheKey);
        return writeJsonOk();
    }

    @GetMapping("/clearAll")
    @RequiresPermissions("monitor:cache:clearAll")
    @ResponseBody
    public HimioResult clearAll() {
        cacheService.clearAll();
        return writeJsonOk();
    }
}
